package club.adger.controller;

import club.adger.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Adger
 * @Date: 2020/08/24/19:47
 * 因为是视图 所以 用的是Controller 不需要 json数据现在
 */
@Controller
@RequestMapping("/view")
//@SessionAttributes(types = {User.class, Address.class})
public class ViewHandler {

    /**
     * 因为 直接 返回 一个视图没什么作用的 需要 放入 map 中 从view 页面 中获取 模型数据
     * @param map
     * @return
     */
    @RequestMapping("/map")
    public String map(Map<String,User> map){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        map.put("user",user);
        return "view";
    }

    @RequestMapping("/model")
    public String model(Model model){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        model.addAttribute("user",user);
        return "view";
    }

    @RequestMapping("/modelAndView")
    public ModelAndView modelAndView(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("view");
        return modelAndView;
    }

    @RequestMapping("/modelAndView2")
    public ModelAndView modelAndView2(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",user);
        View view = new InternalResourceView("/view.jsp");
        modelAndView.setView(view);
        return modelAndView;
    }

    @RequestMapping("/modelAndView3")
    public ModelAndView modelAndView3(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        View view = new InternalResourceView("/view.jsp");
        ModelAndView modelAndView = new ModelAndView(view);
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @RequestMapping("/modelAndView4")
    public ModelAndView modelAndView4(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        ModelAndView modelAndView = new ModelAndView("view");
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @RequestMapping("/modelAndView5")
    public ModelAndView modelAndView5(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        Map<String, User> map = new HashMap<>();
        map.put("user",user);
        ModelAndView modelAndView = new ModelAndView("view",map);
        return modelAndView;
    }

    @RequestMapping("/modelAndView6")
    public ModelAndView modelAndView6(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        Map<String, User> map = new HashMap<>();
        map.put("user",user);
        View view = new InternalResourceView("/view.jsp");
        ModelAndView modelAndView = new ModelAndView(view,map);
        return modelAndView;
    }

    @RequestMapping("/modelAndView7")
    public ModelAndView modelAndView7(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        //后面两个参数是 你到 key value
        ModelAndView modelAndView = new ModelAndView("view","user",user);
        return modelAndView;
    }

    @RequestMapping("/modelAndView8")
    public ModelAndView modelAndView8(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        View view = new InternalResourceView("/view.jsp");
        //后面两个参数是 你到 key value
        ModelAndView modelAndView = new ModelAndView(view,"user",user);
        return modelAndView;
    }

    @RequestMapping("/request")
    public String request(HttpServletRequest httpServletRequest){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        httpServletRequest.setAttribute("user",user);
        return "view";
    }

    @ModelAttribute
    public User getUser(){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        return user;
    }

    @RequestMapping("/ModelAttribute")
    public String modelAttribute(){
        return "view";
    }

    @RequestMapping("/session")
    public String session(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        session.setAttribute("user",user);
        return "view";
    }

    @RequestMapping("/session2")
    public String session2(HttpSession httpSession){
        User user = new User();
        user.setId(1);
        user.setName("Adger");
        httpSession.setAttribute("user",user);
        return "view";
    }

    /**
     * 这里值得说明的是ServletContext 能直接 获取，这个对象没有无参构造，会产生500错误，如果直接获取的话
     * @param request
     * @return
     */
    @RequestMapping("/application")
    public String application(HttpServletRequest request){
        int serverPort = request.getServerPort();
        System.out.println(serverPort);
        String serverName = request.getServerName();
        System.out.println(serverName);
        String servletPath = request.getServletPath();
        System.out.println(servletPath);


        return "view";
    }
}
